package cn.org.eshow.dao.hibernate;

import cn.org.eshow.bean.query.CouponQuery;
import cn.org.eshow.common.dao.EnhancedRule;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.CouponDao;
import cn.org.eshow.model.Coupon;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("couponDao")
public class CouponDaoHibernate extends GenericDaoHibernate<Coupon, Integer> implements CouponDao {

    public CouponDaoHibernate() {
        super(Coupon.class);
    }

    @Override
    public List<Coupon> list(CouponQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return list(rule);
    }

    @Override
    public Page<Coupon> search(CouponQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        if (query != null) {
            rule.setOffset(query.getOffset());
            rule.setPageSize(query.pagesize);
        }
        return page(rule);
    }

    @Override
    public Long count(CouponQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return count(Coupon.class, rule);
    }

    public EnhancedRule setRule(EnhancedRule rule, CouponQuery query) {
        if (query != null) {
            if (query.startAddTime != null) {
                rule.add(Restrictions.ge("addTime", query.startAddTime));
            }
            if (query.endAddTime != null) {
                rule.add(Restrictions.le("addTime", query.endAddTime));
            }
            if (query.startStartTime != null) {
                rule.add(Restrictions.ge("startTime", query.startStartTime));
            }
            if (query.endStartTime != null) {
                rule.add(Restrictions.le("startTime", query.endStartTime));
            }
            if (query.startEndTime != null) {
                rule.add(Restrictions.ge("endTime", query.startEndTime));
            }
            if (query.endEndTime != null) {
                rule.add(Restrictions.le("endTime", query.endEndTime));
            }
            if (query.minMoney != null) {
                rule.add(Restrictions.ge("money", query.minMoney));
            }
            if (query.maxMoney != null) {
                rule.add(Restrictions.le("money", query.maxMoney));
            }
            if (query.minRequirement != null) {
                rule.add(Restrictions.ge("requirement", query.minRequirement));
            }
            if (query.minStock != null) {
                rule.add(Restrictions.ge("stock", query.minStock));
            }
            if (query.maxRequirement != null) {
                rule.add(Restrictions.le("requirement", query.maxRequirement));
            }
            if (!StringUtils.isEmpty(query.scope)) {
                String[] scopes = query.scope.split(",");
                if (scopes.length != 0) {
                    rule.add(Restrictions.in("scope", scopes));
                }
            }
            if (query.enabled != null) {
                rule.add(Restrictions.eq("enabled", query.enabled));
            }
            if (query.userId != null) {
                rule.add(Restrictions.eq("user.id", query.userId));
            }
            if (query.state != null) {
                rule.add(Restrictions.eq("state", query.state));
            }
            if (StringUtils.isNotEmpty(query.states)) {
                String[] vals = StringUtils.split(query.states, ",");
                Integer[] bytes = new Integer[vals.length];
                for (int i = 0; i < vals.length; i++) {
                    bytes[i] = Integer.valueOf(vals[i].trim());
                }
                rule.add(Restrictions.in("state", bytes));
            }
            if (!StringUtils.isEmpty(query.getOrder())) {
                rule.addOrder(query.getDesc() ? Order.desc(query.getOrder()) : Order.asc(query.getOrder()));
            }
        }
        return rule;
    }
}
